package sz.szsmk.citizencard.ui.adapter

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import kotlinx.android.synthetic.main.business_item.view.*
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes.ShopListInfosBean
import sz.szsmk.citizencard.util.GlideRoundTransform
import sz.szsmk.citizencard.util.JumpUtils
import sz.szsmk.citizencard.util.StringUtils
import sz.szsmk.citizencard.util.Utils

/**
 *  author : hyt
 *  date : 2020/10/16
 *  description :
 */
class NetSearchAdapter: BaseQuickAdapter<ShopListRes.ShopListInfosBean, BaseViewHolder>(R.layout.business_item) {

    var key = "1"

    override fun convert(helper: BaseViewHolder?, item: ShopListRes.ShopListInfosBean?) {

        helper?.setText(R.id.business_adress, item?.address)
        val i = (item?.distance)?.div(10) as Int
        val formatPrice = StringUtils.decimalFormatPrice(i)
        helper?.setText(R.id.business_distance, "${formatPrice}km")
        Glide.with(mContext)
                .load(item?.picUrl)
                .error(R.drawable.zhanweitu)
                .placeholder(R.drawable.zhanweitu)
                .centerCrop()
                .override(100, 100)
                .transform(GlideRoundTransform(mContext))
                .into(helper?.itemView?.business_iv)
        helper?.setText(R.id.business_shop, item?.shopName)

        helper?.setText(R.id.bussness_tell, "")
        item?.telephone?.let {
            helper?.setText(R.id.bussness_tell, "联系商家")
        }
        helper?.itemView?.bussness_tell?.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            val data = Uri.parse("tel:" + item.telephone)
            intent.data = data
            mContext.startActivity(intent)
        }
        helper?.itemView?.setOnClickListener {
                if (key == "net") {
                    val longitude: Double = item?.longitude.toString().toDouble()
                    val latitude: Double = item?.latitude.toString().toDouble()
                    val bundle = Bundle()
                    bundle.putInt("id", item?.id)
                    bundle.putDouble("latitude", latitude)
                    bundle.putDouble("longitude", longitude)
                    JumpUtils.jumptoNatvie(mContext, "netdetail", 0, bundle)
                } else {
                    val shopListInfosBean: ShopListInfosBean = item
                    //打开地图导航
//                    Utils.startMap(mContext, shopListInfosBean)
                    Utils.startMapBylocationByBaiduCorSys(mContext, shopListInfosBean.latitude as Double, shopListInfosBean.longitude as Double, shopListInfosBean.shopName)
                }
        }
    }
}